package com.txsj.scan.widget.popup;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.txsj.scan.R;
import com.txsj.scan.bean.EmployeeBean;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by admin on 2018/3/31.
 */

public class PopupList {
    Context mContext;
    PopupWindow mPopup;
    ListView lv_popup;
    LinearLayout lin_popup_root;
    View showView;//popupWindow  要显示到某个View的位置
    View rootView;//要显示的popup的View
    List<?> list = new ArrayList<>();
    PopupListAdapter adapter;

    public PopupList(Context mContext, View view, List<?> datas) {
        this.mContext = mContext;
        this.showView = view;
        this.list = datas;
    }

    public PopupList builder() {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        rootView = layoutInflater.inflate(R.layout.popup_listview, null);
        lv_popup = (ListView) rootView.findViewById(R.id.lv_popup);
        lin_popup_root = (LinearLayout) rootView.findViewById(R.id.lin_popup_root);
        // 为自定义的对话框设置自定义布局
        mPopup = new PopupWindow(rootView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopup.setFocusable(true);
        mPopup.setBackgroundDrawable(new BitmapDrawable());
        // 设置允许在外点击消失
        mPopup.setOutsideTouchable(true);
//        mPopup.setAnimationStyle(R.style.anim_popup_top);
        lin_popup_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopup.dismiss();
            }
        });
        adapter = new PopupListAdapter();
        lv_popup.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return this;
    }

    public PopupList setPopupItemClick(final AdapterView.OnItemClickListener listener) {
        lv_popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPopup.dismiss();
                listener.onItemClick(parent, view, position, id);
            }
        });
        return this;
    }

    public PopupList setOnDismissListener(PopupWindow.OnDismissListener listener) {
        mPopup.setOnDismissListener(listener);
        return this;
    }

    public boolean isShow() {
        if (mPopup != null && mPopup.isShowing()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 显示PopupWindow
     *
     * @return
     */
    public PopupList show() {
//        //获取点击View的坐标
        int[] location = new int[2];
        showView.getLocationOnScreen(location);
        if (Build.VERSION.SDK_INT < 24) {
            mPopup.showAsDropDown(showView, location[0] + showView.getWidth(), 0);
        } else {
            mPopup.showAsDropDown(showView);
        }
        return this;
    }


    /**
     * 计算出来的位置，y方向就在anchorView的上面和下面对齐显示，x方向就是与屏幕右边对齐显示
     * 如果anchorView的位置有变化，就可以适当自己额外加入偏移来修正
     *
     * @param anchorView  呼出window的view
     * @param contentView window的内容布局
     * @return window显示的左上角的xOff, yOff坐标
     */
    private static int[] calculatePopWindowPos(final View anchorView, final View contentView) {
        final int windowPos[] = new int[2];
        final int anchorLoc[] = new int[2];
        // 获取锚点View在屏幕上的左上角坐标位置
        anchorView.getLocationOnScreen(anchorLoc);
        final int anchorHeight = anchorView.getHeight();
        // 获取屏幕的高宽
        final int screenHeight = ScreenUtils.getScreenHeight(anchorView.getContext());
        final int screenWidth = ScreenUtils.getScreenWidth(anchorView.getContext());
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 计算contentView的高宽
        final int windowHeight = contentView.getMeasuredHeight();
        final int windowWidth = contentView.getMeasuredWidth();
        // 判断需要向上弹出还是向下弹出显示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] - windowHeight;
        } else {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] + anchorHeight;
        }
        return windowPos;
    }


    class PopupListAdapter extends BaseAdapter {

        public PopupListAdapter() {

        }

        @Override
        public int getCount() {
            if (list != null && list.size() > 0) return list.size();
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (viewHolder == null) {
                convertView = View.inflate(mContext, R.layout.item_array_list, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            if (list != null) {
                if (list.get(position) instanceof EmployeeBean) {
                    EmployeeBean mEmployee = (EmployeeBean) list.get(position);
                    viewHolder.tv_item.setText(mEmployee.getEmployee_name());
                } else if (list.get(position) instanceof WarehouseBean) {
                    WarehouseBean warehouseBean = (WarehouseBean) list.get(position);
                    viewHolder.tv_item.setText(warehouseBean.getWarehouse_name());
                } else {
                    HashMap<String, String> map = ((HashMap<String, String>) list.get(position));
                    viewHolder.tv_item.setText(map.get("stateName"));
                }
            }
            return convertView;
        }


        class ViewHolder {
            TextView tv_item;

            public ViewHolder(View viewItem) {
                tv_item = viewItem.findViewById(R.id.tv_item);
            }
        }
    }


}
